import math, audioop

class Goertzel:

    def GetPower(self,data,frequency, samplerate):
        self.__coeff = 2*math.cos(2*math.pi*frequency/samplerate);
        
        s =0
        s_prev = 0
        s_prev2 = 0
        for i in range(0, len(data)/2):
            x = audioop.getsample(data, 2, i)
            
            s = x + self.__coeff*s_prev - s_prev2;
            s_prev2 = s_prev;
            s_prev = s;
        power = s_prev2*s_prev2 + s_prev*s_prev - self.__coeff*s_prev2*s_prev;
        return power


